package com.jinglin.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinglin.controller.ABaseController;
import com.jinglin.entity.enums.VideoFileUpdateTypeEnum;
import com.jinglin.entity.enums.VideoRecommendTypeEnum;
import com.jinglin.entity.enums.VideoStatusEnum;
import com.jinglin.entity.po.VideoInfo;
import com.jinglin.entity.po.VideoInfoFile;
import com.jinglin.entity.po.VideoInfoFilePost;
import com.jinglin.entity.po.VideoInfoPost;
import com.jinglin.entity.vo.PaginationResultVO;
import com.jinglin.entity.vo.ResponseVO;
import com.jinglin.exception.BusinessException;
import com.jinglin.service.VideoInfoFilePostService;
import com.jinglin.service.VideoInfoPostService;
import com.jinglin.service.VideoInfoService;
import jdk.jfr.StackTrace;
import org.apache.coyote.Response;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@RestController
@RequestMapping("/videoInfo")
public class VideoInfoController extends ABaseController {
    @Autowired
    private VideoInfoPostService videoInfoPostService;
    @Autowired
    private VideoInfoFilePostService videoInfoFilePostService;
    @Autowired
    private VideoInfoService videoInfoService;

    @PostMapping("/loadVideoList")
    public ResponseVO loadVideoList(Integer status, Integer pageNo, String videoNameFuzzy) {
        Page<VideoInfoPost> pageObj = new Page<>(pageNo != null ? pageNo : 0, 10);

        LambdaQueryWrapper<VideoInfoPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(status != null, VideoInfoPost::getStatus, status);
        Page<VideoInfoPost> pageInfo = videoInfoPostService.page(pageObj, wrapper);
        PaginationResultVO<VideoInfoPost> resultVO = new PaginationResultVO<>();
        List<VideoInfoPost> list = pageInfo.getRecords();
        for (VideoInfoPost videoInfoPost : list) {
            VideoInfo videoInfo = videoInfoService.getById(videoInfoPost.getVideoId());
            if (videoInfo != null){
               // videoInfoPost.setRecommendType(videoInfo.getRecommendType());
                BeanUtils.copyProperties(videoInfo,videoInfoPost);
            }

        }
        resultVO.setList(list);
        resultVO.setPageNo((int) pageInfo.getCurrent());
        resultVO.setPageSize((int) pageInfo.getSize());
        resultVO.setPageTotal((int) pageInfo.getPages());
        resultVO.setTotalCount((int) pageInfo.getTotal());
        return getSuccessResponseVO(resultVO);
    }

    /*
     * 审核稿件，成功后将数据copy到正式表中
     * */
    @PostMapping("/auditVideo")
    public ResponseVO auditVideo(@NotEmpty String videoId, @NotEmpty Integer status, @RequestParam(value = "reason", required = false) String reason) {
        videoInfoPostService.auditVideo(videoId, status, reason);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/recommendVideo")
    public ResponseVO recommendVideo(String videoId) {
        VideoInfo one = videoInfoService.getById(videoId);
        if (one != null) {
            if (one.getRecommendType() == null){
                one.setRecommendType(VideoRecommendTypeEnum.NO_RECOMMEND.getType());
            }
            one.setRecommendType(one.getRecommendType().equals(VideoRecommendTypeEnum.NO_RECOMMEND.getType())
                            ? VideoRecommendTypeEnum.RECOMMEND.getType()
                            : VideoRecommendTypeEnum.NO_RECOMMEND.getType()
            );
            videoInfoService.updateById(one);
        }
        return getSuccessResponseVO(null);
    }
}
